close all
clear all
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  Main for the demo of finding F  %
%  using isotropic scaling transf  %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Load Images
left_image  = imread('library1.jpg');
right_image = imread('library2.jpg');
%% Get Corresponding Points
%n = 20;
%[x,xp] = select_corr(n,left_image,right_image);%the human user will select n points of correspondence
%% ... or Load Data Points
x_mat = load('x.mat'); x = x_mat.x;
xp_mat = load('xp.mat'); xp = xp_mat.xp;
%% Show Points and Correspondence
imshow([left_image zeros(size(left_image,1),5,3) right_image]); hold on;
title('Corresponding Points')
plot(x(1:5:end,1), x(1:5:end,2), '*y');
plot(xp(1:5:end,1)+size(left_image,2)+5, xp(1:5:end,2), '*y');
line([x(1:5:end,1) xp(1:5:end,1)+size(left_image,2)+5]', [x(1:5:end,2) xp(1:5:end,2)]', 'Color', 'r');
pause;               %press something!
%% Obtain F, Normalized
F = Ffromxs_norm(x,xp)
%% Epipoles
[U, ~, V] = svd(F);
e1 = V(:,3);
e1 = e1./(e1(3));%normalized epipole 1    
e2 = U(:,3);
e2 = e2./(e2(3));%normalized epipole 2 
%%%%%%%%this satisifies Fe1=0; F'e2=0
%% Ploting
close all
subplot(1,2,1), imagesc(left_image); title('Left Image, click to see corresponing epipolar line');colormap(gray);axis image;
subplot(1,2,2), imagesc(right_image); title('Right Image, epipolar lines');colormap(gray);axis image;

%%%Depending on the type of images we may want to plot the epipoles ...

for i=1:length(x)
    subplot(1,2,1)%left image
    [px, py] = ginput(1);
    hold on; plot(px,py,'ro');
    m = [px py 1];%to homogeneous
    l = F*m'; %points in right image
    subplot(1,2,2)
    plotline(l);
end

